﻿//===============================================================================
// Apolineo Patterns & Practices LeanEngine Framework
//===============================================================================
// Copyright © Apolineo Desenvolvimento de Sistemas Ltda.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Windows.Forms;
using LeanEngine.SalesConcept.Entity;
using LeanEngine.Entity;
using LeanEngine.Extensions;
using System.ComponentModel;

namespace LeanEngine.SalesConcept.Windows.Control
{
    public partial class MaintenanceControlOfCustomer : UserControl, IMaintenanceControl<Customer>
    {
        #region Constructor Members

        public MaintenanceControlOfCustomer()
        {
            InitializeComponent();
        }

        #endregion

        #region Property Members

        private Customer CurrentCustomer{ get; set; }

        //Através do controle de integração dos validadores com controles de tela
        //PropertyValidatorProvider (LeanEngine.Validation.Windows.Forms) o lob se encarregra de
        //executar os validadores passando os valores do controle e retornando
        //na propriedade ErrorMessages as mensagens dos erros (caso exista)
        [Browsable(false)]
        public string ErrorMessages
        {
            get { return this.propertyValidatorProvider1.ErrorMessages; }
        }

        #endregion

        #region IMaintenanceControl<Customer> Members

        public Customer GetControlsValues()
        {
            if (this.CurrentCustomer.IsNull()) this.CurrentCustomer = new Customer();

            this.CurrentCustomer.Name = this.txtName.Text;
            this.CurrentCustomer.DocumentNumber = this.txtDocumentNumber.Text;
            this.CurrentCustomer.Address = this.txtAddress.Text;
            this.CurrentCustomer.Email = this.txtEmail.Text;
            this.CurrentCustomer.Birthdate = this.dtpBirthDate.Value;

            return this.CurrentCustomer;
        }

        public void SetControlsValues(Customer value)
        {
            this.CurrentCustomer = value;

            this.txtName.Text = this.CurrentCustomer.Name;
            this.txtDocumentNumber.Text = this.CurrentCustomer.DocumentNumber;
            this.txtAddress.Text = this.CurrentCustomer.Address;
            this.txtEmail.Text = this.CurrentCustomer.Email;
            this.dtpBirthDate.Value = this.CurrentCustomer.Birthdate;
        }

        public void ClearControls()
        {
            this.txtName.Text = string.Empty;
            this.txtDocumentNumber.Text = string.Empty;
            this.txtAddress.Text = string.Empty;
            this.txtEmail.Text = string.Empty;
            this.dtpBirthDate.Value = new DateTime(1980, 1, 1);

            this.txtName.Focus();
        }

        #endregion
    }
}
